/*
 * @FileDescription:
 * @Author: 茆生涛
 * @Date: 2021-07-20 14:32:48
 * @LastEditors: mst
 * @LastEditTime: 2021-10-27 17:56:24
 */
import Vue from 'vue'
import Router from 'vue-router'
import Md5 from '@/utils/md5'
// const HelloWorld = () => import('@/components/HelloWorld')

Vue.use(Router)
// 解决vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => err)
}
const routesArr = [
  // {
  //   path: '/',
  //   name: 'HelloWorld',
  //   component: () => import('@/components/HelloWorld')
  // },
  {
    path: '/',
    name: 'home',
    component: () => import('@/pages/home/home.vue'),
    children: [
      {
        path: '/random',
        name: 'random',
        component: () => import('@/pages/random/index.vue')
      },
      {
        path: '/randomList',
        name: 'randomList',
        component: () => import('@/pages/randomList/index.vue')
      },
      {
        path: '/my',
        name: 'my',
        component: () => import('@/pages/my/index.vue')
      }
    ]
  },
  {
    path: '/dishDetail',
    name: 'dishDetail',
    component: () => import('@/pages/dishDetail/index.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/pages/login/login.vue')
  }
]

const router = new Router({
  routes: routesArr
})

router.beforeEach((to, from, next) => {
  const user = JSON.parse(localStorage.getItem('randomMenuUser'))
  if (user && user.name === 'ml' && user.password === Md5('123456', 'm', true)) {
    next()
  } else {
    if (to.name === 'Login') {
      next()
    } else {
      next({ name: 'Login' })
    }
  }
})

export default router
